//
//  NSAttributeString+Extension.swift
//  Weibo22
//
//  Created by Apple on 16/12/12.
//  Copyright © 2016年 Apple. All rights reserved.
//

import UIKit

extension NSAttributedString {
    //  根据表情模型创建表情模型的对应富文本
    class func attributeStringWithEmoticon(emoticon: HMEmoticon, font: UIFont) -> NSAttributedString {
        
        //  1. 通过图片名(图片路径)创建UIImage对象
        let image = UIImage(named: emoticon.path!)
        
        //  2. 根据UIImage对象创建文本附件(NSTextAttachment)
        let attachment = HMTextAttachment()
        //  设置文本附件的图片
        attachment.image = image
        //  设置文本附件对应的表情模型
        attachment.emoticon = emoticon
        
        //  文字高度
        let lineHeight = font.lineHeight
        //  设置图片大小 (设置bounds 会影响子控件的布局)
        attachment.bounds = CGRect(x: 0, y: -4, width: lineHeight, height: lineHeight)
        
        //  3. 根据富文本附件创建富文本(NSAttributedString)
        let attributedStr = NSAttributedString(attachment: attachment)
        

        return attributedStr
    
    }

}
